package cn.edu.lingnan.controller;

import cn.edu.lingnan.domain.dto.DepositDto;
import cn.edu.lingnan.domain.dto.WthdrawDto;
import cn.edu.lingnan.domain.pojo.Accounts;
import cn.edu.lingnan.domain.pojo.Atm;
import cn.edu.lingnan.domain.pojo.Result;
import cn.edu.lingnan.domain.vo.Card_UserVo;
import cn.edu.lingnan.domain.vo.CardsVo;
import cn.edu.lingnan.domain.vo.UserVo;
import cn.edu.lingnan.service.AtmService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/atm")
@RequiredArgsConstructor
public class AtmController {

    private final AtmService atmService;



    //获取ATM机信息
    @GetMapping("/getAtmInfo")
    public Result<List<Atm>> getAtmInfo(){
        log.info("getAtmInfo");
        List<Atm> atmList = atmService.getAtmInfo();
        return Result.success(atmList);
    }
    //根据ATM机编号获取ATM机信息
    @GetMapping("/getAtmInfoById")
    public Result<Atm> getAtmInfoByAid( @RequestParam ("id") Integer id){
        log.info("getAtmInfoByAid");
        Atm atm = atmService.getAtmInfoByAid(id);
        return Result.success(atm);
    }

    //用户输入用户信息
    @PostMapping("/inputUserInfo")
    public Result<UserVo> inputUserInfo(@RequestParam("name") String name,@RequestParam("password") String password){
        log.info("inputUserInfo");
        UserVo userVo = atmService.inputUserInfo(name,password);
        if(userVo == null){
            return Result.error("用户名或密码错误");
        }
        return Result.success( userVo);
    }
    //查询现金兑换卡
    @GetMapping("/getCashExchangeCard")
    public Result<List<CardsVo>> getCashExchangeCard(){
        log.info("getCashExchangeCard");
        List<CardsVo> cardsVoList = atmService.getCashExchangeCard();
        return Result.success(cardsVoList);
    }

    //验证银行卡密码
    @GetMapping("/verifyBankCardPassword")
    public Result<Card_UserVo> verifyBankCardPassword(@RequestParam("cardNumber") String cardNumber, @RequestParam("cardPassword") String cardPassword) {
        log.info("verifyBankCardPassword bankCardNumber:{}, bankCardPassword:{}",  cardNumber, cardPassword);

        Card_UserVo card_userVo = atmService.verifyBankCardPassword(cardNumber, cardPassword);
        if(card_userVo == null){
            return Result.error("密码错误");
        }
         return  Result.success(card_userVo);
    }

    //取款
    @PostMapping("/withdraw")
    public Result<String> withdraw(@RequestBody WthdrawDto wthdrawDto) {
        log.info("withdraw, wthdrawDto:{}", wthdrawDto);
        String result = atmService.withdraw(wthdrawDto);
        return Result.success(result);
    }

    //根据账户id查询账户信息
    @GetMapping("/getAccountInfoById")
    public Result<Accounts> getAccountInfoById(@RequestParam("accountId") Integer accountId) {
        log.info("getAccountInfoById, accountId:{}", accountId);
        Accounts accounts = atmService.getAccountInfoById(accountId);
        return Result.success(accounts);
    }
    //存款
    @PostMapping("/deposit")
    public Result<String> deposit( @RequestBody DepositDto depositDto) {
        log.info("deposit, depositDto:{}", depositDto);
        String result = atmService.deposit(depositDto);
        return Result.success(result);
    }

    //导出为Excel
    @GetMapping("/exportExcel")
    public Result<String> exportExcel( @RequestParam("accountId")  Integer accountId,
                                      HttpServletResponse response) throws IOException {
        log.info("exportExcel, userId:{}", accountId);
        String result = atmService.exportExcel(accountId, response);
        return Result.success("导出成功");
    }
}
